//
//  YYEComposeToolbar.swift
//  weibo14
//
//  Created by mac on 16/3/12.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

/// 定义不同按钮的枚举
///
/// - Picture:  图片
/// - Mention:  @
/// - Trend:    话题
/// - Emoticon: 表情图标
/// - Add:      加号
enum YYEComposeToolbarButtonType: Int {
    case Picture = 0, Mention, Trend, Emoticon, Add
}


class YYEComposeToolbar: UIStackView {

    
    // 定义一个回调的闭包
    var btnClickCallBack: ((btn:UIButton)->())?
    
    var emoticonBut:UIButton?
    
    // 定义个标志位 控制是图标
    var isSystemKeyboard:Bool = true {
        didSet {
            if !isSystemKeyboard {
                emoticonBut?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                
                emoticonBut?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            } else {
                emoticonBut?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonBut?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
            
        }
    }
    
//    let button = self.viewWithTag(HMComposeToolbarButtonType.Emoticon.rawValue)! as! UIButton
//    var imageNamed = "compose_keyboardbutton_background"
//    if isSystemKeyboard {
//    // 是系统键盘,显示笑脸
//    imageNamed = "compose_emoticonbutton_background"
//    }
//    button.setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
//    button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        // 容器视图是不具在显示的功能的
        // backgroundColor = UIColor.redColor()
        // 设置大小相等并且填充父控件
        distribution = .FillEqually
        
        addButtom(.Picture,imageName: "compose_toolbar_picture")
        addButtom(.Mention, imageName: "compose_mentionbutton_background")
        addButtom(.Trend, imageName: "compose_trendbutton_background")
        emoticonBut = addButtom( .Emoticon,imageName: "compose_emoticonbutton_background")
        addButtom(.Add, imageName: "compose_add_background")
    }
    
    private func addButtom(type:YYEComposeToolbarButtonType,imageName:String) ->UIButton {
    
        let btn = UIButton()
        
        btn.tag = type.rawValue
        
        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置不同状态的图片
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置背景图
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        self.addArrangedSubview(btn)
        
        return btn
    }
    
    @objc private func btnClick(button:UIButton) {
    
        btnClickCallBack?(btn:button)
    
    }
    
}
